package com.larry.java2;

import java.util.concurrent.TimeUnit;

/**
 * @author larry
 * @Title: jvm_dc
 * @Package com.larry.java2
 * @Description: clinit会造成死锁
 * @date 7/28/219:39 PM
 */

class StaticA{
    static {
        try { TimeUnit.SECONDS.sleep(1); }catch (InterruptedException e){ e.printStackTrace(); }
        try {
            Class.forName("com.larry.java2.StaticB");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println("A init ok");
    }
}

class StaticB{
    static {
        try { TimeUnit.SECONDS.sleep(1); }catch (InterruptedException e){ e.printStackTrace(); }
        try {
            Class.forName("com.larry.java2.StaticA");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println("B init ok");
    }
}

public class StaticDeadLockDemo extends Thread{
    private char flag;
    public StaticDeadLockDemo(char flag){
        this.flag = flag;
        this.setName("thread" + flag);
    }

    @Override
    public void run() {
        try {
            Class.forName("com.larry.java2.Static" + flag);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println(getName() + "over");
    }

    public static void main(String[] args) {
        StaticDeadLockDemo a = new StaticDeadLockDemo('A');
        a.start();
        StaticDeadLockDemo b = new StaticDeadLockDemo('B');
        b.start();

    }
}
